package cn.edu.scau.cmi.chenbingquan.client;

import cn.edu.scau.cmi.chenbingquan.abstractFactory.factory.AbstractFactory;
import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ComboBox;
import javafx.scene.control.ListView;
import javafx.scene.image.Image;
import javafx.scene.layout.Pane;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.text.Text;
import javafx.stage.Stage;

// 抽象工厂模式用户类
public class AbstractFactoryClient extends Application {
	private Pane root = new Pane();
	private ComboBox<String> brandCB;	// 品牌下拉框
	private ComboBox<String> meatCB;	// 肉类下拉框
	private Button button;		// 创建按钮
	private ListView<Text> lv;	// 文本显示列表(放文本用于改变字体大小颜色)
	
	public static void main(String[] args) {
		launch(args);
	}

	@Override
	public void start(Stage stage) {
		
		root.setStyle("-fx-background-color: linear-gradient(to bottom right, ANTIQUEWHITE, CORNFLOWERBLUE)");
		
		brandCB = new ComboBox<>();
		brandCB.getItems().addAll("Wens", "Tianbang", "Aonong");
		brandCB.setVisibleRowCount(3);
		brandCB.setPromptText("请选择品牌");
		brandCB.setLayoutX(60);
		brandCB.setLayoutY(70);
		
		meatCB = new ComboBox<>();
		meatCB.getItems().addAll("beef", "pork", "chicken");
		meatCB.setVisibleRowCount(3);
		meatCB.setPromptText("请选择生产的肉类");
		meatCB.setLayoutX(200);
		meatCB.setLayoutY(70);
		
		button = new Button("创建");
		button.setLayoutX(380);
		button.setLayoutY(70);
		
		lv = new ListView<>();
		lv.getItems().add(new Text("抽象工厂模式生产的肉类行为如下："));
		lv.setPrefSize(480, 280);
		lv.setFixedCellSize(30);
		lv.setLayoutX(60);
		lv.setLayoutY(150);
		root.getChildren().addAll(brandCB, meatCB, button, lv);
		
		Scene scene = new Scene(root, 600, 450);
		stage.setScene(scene);
		stage.getIcons().add(new Image("cn/edu/scau/cmi/chenbingquan/image/窗口左上角图标.png"));
		stage.setTitle("抽象工厂模式");
		stage.setResizable(false);
		stage.show();
		
		// 事件
		// 创建按钮点击事件：产生肉类并输出信息
		button.setOnMouseClicked(e -> {
			if(brandCB.getValue() == null || 		// 没有选择品牌或肉类时
					meatCB.getValue() == null) {
				showWrongStage();
			} else {
				String brand = brandCB.getValue();		// 获取选择的品牌
				String type = meatCB.getValue();		// 获取选择的肉类
				String info = "";
				switch(type) {
					case "beef": 
						info = AbstractFactory.getFactory(brand).createBeef().getInfo();
						break;
					case "chicken":
						info = AbstractFactory.getFactory(brand).createChicken().getInfo();
						break;
					case "pork":
						info = AbstractFactory.getFactory(brand).createPork().getInfo();
						break;
				}
				Text text = new Text(info);
				text.setFill(Color.ORANGERED);
				lv.getItems().add(text);		
			}
		});
	}
	
	// 没有选择肉类时出现错误窗口
	public void showWrongStage() {
					
		VBox vBox = new VBox(60);
		vBox.setAlignment(Pos.CENTER);
		vBox.setStyle("-fx-background-color: ANTIQUEWHITE");
					
		Text text = new Text("请先输入品牌或肉类!!");
		text.setStyle("-fx-font-size: 25; -fx-fill: orange"); 
					
		Button button = new Button("确定");
		button.setPrefSize(60, 30);
		vBox.getChildren().addAll(text, button);
					
		Stage stage = new Stage();
		Scene scene = new Scene(vBox, 300, 250);
		stage.setScene(scene);
		stage.show();
					
		// 事件
		button.setOnMouseClicked(e -> stage.close() ); 		// 关闭窗口
	}
}
